package com.byzx.wine.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.byzx.wine.service.IRoleService;
import com.byzx.wine.service.ISyuserService;


import com.byzx.wine.util.Page;


import com.byzx.wine.po.Syuser;


@Controller
@RequestMapping(value = "/user")
public class SyuserAction {
	@Autowired
	private ISyuserService syuserService;
	@Autowired
	private IRoleService iRoleService;

//显示页面
	@RequestMapping("user1")
	public String login(HttpServletResponse response, HttpServletRequest request) {
		return "user/user";
	}

	@RequestMapping("finduser")
	@ResponseBody
	public Map<String, Object> findPage(Model model, Integer iDisplayLength,
			Integer sEcho, Integer iDisplayStart) {
		if (sEcho == null) {
			sEcho = 1;
		}
		if (iDisplayLength == null) {
			iDisplayLength = 10;
		}
		if (iDisplayStart == null) {
			iDisplayStart = 1;
		}
		Map<String, Object> map = new HashMap<String, Object>();
		Page<Map<String, Object>> page = new Page<Map<String, Object>>(
				iDisplayStart, iDisplayLength, map);
		Page<Map<String, Object>> page1 = syuserService.findPage(page);

		map.put("sEcho", sEcho);
		map.put("iTotalRecords", page1.getTotalCount());
		map.put("iTotalDisplayRecords", page1.getTotalCount());
		map.put("aaData", page1.getList());
		return map;
	}
//添加
	@RequestMapping("useradd")
	@ResponseBody
	public Map<String, Object> useradd(Syuser user) {
		System.out.println(user.getName());
		System.out.println(user.getId());
		System.out.println(user.getPassword());
		System.out.println("=========");
		Map<String, Object> map = new HashMap<String, Object>();
		int num = syuserService.insert(user);
		map.put("num", num);
		return map;
	}
//显示数据
	@RequestMapping("/selectByPrimaryKey")
	@ResponseBody
	public Map<String, Object> selectByPrimaryKey(Integer id) {
		Syuser sy=syuserService.selectByPrimaryKey(id);
		Map<String,Object> map =new HashMap<String,Object>();
		map.put("sy", sy);
		return map;
	}
//修改数据
	@RequestMapping("/userupdate")
	@ResponseBody
	public Map<String, Object> userupdate(Syuser user) {
   System.out.println(user.getId());
   Integer num=syuserService.updateByPrimaryKey(user);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("num", num);
		return map;
	}

	@RequestMapping("/userdelete")
	@ResponseBody
	public Map<String, Object> userdelete(Integer id) {
		Map<String, Object> map = new HashMap<String, Object>();
		int num = syuserService.deleteByPrimaryKey(id);
		map.put("num", num);
		return map;
	}

	@RequestMapping("/userdeletes")
	@ResponseBody
	public Map<String, Object> roledeletes(String ids) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("flag", syuserService.deleteAll(ids));
		return map;
	}
	
	@RequestMapping("selectRole")
	@ResponseBody
	public Map<String, Object> findRole(){
		Map<String, Object> map = new HashMap<String, Object>();
		List<Map<String, Object>> list=iRoleService.findRole();
		map.put("list", list);
		return map;
	}
	
	@RequestMapping("dealSyuserrole")
	@ResponseBody
	public Map<String, Object> dealSyuserrole(Integer userid,String ids){
		Integer s = iRoleService.dealSyuserrole(userid, ids);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("s", s);
		return map;
	}

}
